
(function () {
    var core = ecui,
        util = core.util,
        ui = ecui.ui,
        ext = core.ext,
        dom = ecui.dom;

    /*
     * 切换费用时，计算费用总计
     * options    属性：
     * optId       操作的记录id
     * detailId    获取详情信息的记录id
     * url         获取详情信息的接口路径
     * container   dialog容器id
     * target      dialog 的 内容target 模板名称
     *
     */
    ext.budgetSort = {
        constructor: function (value, options) {
            this._svalue = value;
            this._oOptions = options;
        },
        Events: {
            itemChange: function () {
                ext.budgetSort.sort.call(this);
            },
            change: function (event) {
                ext.budgetSort.sort.call(this);
            },
            ready: function () {
                ext.budgetSort.sort.call(this);
            }
        },
        sort: function () {
            var cont = this._svalue === 'add' ? dom.previous(this.getMain()) : this.getParent().getParent().getMain();
            dom.children(cont).forEach(function (item, index) {
                dom.first(item).innerHTML = '预算明细' + (index + 1);
            });
        }
    };

    Object.assign(
        NS.data,
        {
        }
    );
    Object.assign(
        NS.ui,
        {
            VenueTypeRadio: core.inherits(
                ui.Radio,
                {
                    $click: function (event) {
                        ui.Radio.prototype.$click.call(this, event);
                        this.changeHandler();
                    },
                    changeHandler: function () {
                        var form = this.getInput().form,
                            yard = form.yardId[0].getControl(),
                            externalYard = form.yardId[1].getControl(),
                            yardName = form.yardName.getControl();
                        if (this.getValue() === '1') {
                            yard.getParent().hide();
                            yard.disable();
                            externalYard.getParent().show();
                            externalYard.enable();
                            yardName.setValue(externalYard.getText());
                        } else {
                            yard.getParent().show();
                            yard.enable();
                            externalYard.getParent().hide();
                            externalYard.disable();
                            yardName.setValue(yard.getText());
                        }
                    },
                    init: function () {
                        ui.Radio.prototype.init.call(this, event);
                        util.timer(function () {
                            if (this.isChecked()) {
                                this.changeHandler();
                            }
                        }, 10, this);
                    }
                }
            ),
            VenueSelectEdit: core.inherits(
                mao.ui.SelectEdit,
                {
                    onchange: function (event) {
                        mao.ui.SelectEdit.prototype.onchange.call(this, event);
                        console.log(event);
                        this.getInput().form.elements.yardName.getControl().setValue(event.item._sText);
                    }
                }
            ),
            SaveProjectBtn: core.inherits(
                ui.Button,
                function (el, options) {
                    ui.Button.call(this, el, options);
                    this._sOptId = options.optId;
                },
                {
                    onclick: function () {

                        var data = {},
                            valid = ecui.esr.parseObject(document.forms.editBrandProjectForm, data);

                        if (!valid) {
                            return;
                        }
                        if (!data.projectTypeList.length) {
                            ecui.tip('warn', '请选择项目类型');
                            return;
                        }
                        this.request(
                            'data@FORM /erp-management/label-project/save?editBrandProjectForm',
                            function () {
                                var data = ecui.esr.getData('data');
                                if (data instanceof Object) {
                                    ecui.tip('success', this._sOptId ? '修改成功' : '添加成功');
                                    history.go(-1);
                                }
                            }.bind(this),
                            function () {
                                return false;
                            }.bind(this)
                        );
                    }
                },
                ui.Submit
            )
        }
    );
    ecui.esr.addRoute('edit', {
        model: function (context, callback) {
            var model = [
                'detail@GET /erp-management/label-project/detail/${id}',
                'venues@GET /erp-management/venue/select-list-user',
                'sponsor@GET /erp-management/customer/sponsor/statement/select-list',
                'externalVenues@GET /erp-management/external/venue/select-list'
            ];
            if (!context.id) {
                model.splice(0, 1);
            }
            ecui.esr.request(model, callback);
            return false;
        },
        weight: 1,
        frame: true,
        onbeforerequest: function (context) {
            context.num = mao.util.getTimeRandom();
        },
        onbeforerender: function (context) {
            mao.info.baseInfo.projectType = mao.info.baseInfo.projectType || [];
            var venueMap = {};
            mao.venues.forEach(function (item) {
                venueMap[item.id] = item;
            });
            NS.data.venues = context.venues.map(function (item) {
                return venueMap[item.id];
            });
        },
        onafterrender: function (context) {
        }
    });
}());

